# Project: From Home Base to Swing States: The Evolution of Digital Advertising 
#          Strategies during the 2020 US Presidential Primary
# Authors: NaLette Brodnax and Piotr Sapiezynski

library(tidyverse)
library(stargazer)

ads_all <- read_csv('replication_data_aggregate.csv') 

# Candidates who dropped
drop_candidates <- c("Amy Klobuchar", "Andrew Yang", 
                     "Bernie Sanders", "Cory Booker", "Elizabeth Warren", 
                     "Joe Biden", "John K. Delaney", "Marianne Williamson", 
                     "Michael Bennet", "Mike Bloomberg", "Pete Buttigieg", 
                     "Tom Steyer", "Tulsi Gabbard")

ads_dropped <- ads_all %>% 
  filter(!candidate %in% drop_candidates) 

# Candidates who remained
drop_candidates <- c("Beto O'Rourke", "Eric Swalwell", 
                     "Governor Steve Bullock", "Jay Inslee", "John Hickenlooper",
                     "Julián Castro", "Kamala Harris", "Kirsten Gillibrand",
                     "Mayor Wayne Messam", "Mike Gravel", "Richard Ojeda",
                     "Seth Moulton", "Tim Ryan")

ads_remained <- ads_all %>% 
  filter(!candidate %in% drop_candidates)

# Home estimates by group
fit1 <- lm(budget_frac_per_resident ~ is_home + factor(state) +
             factor(candidate) -1, 
           data = ads_all)

fit2 <- lm(budget_frac_per_resident ~ is_home + factor(state) +
             factor(candidate) -1, 
           data = ads_dropped)

fit3 <- lm(budget_frac_per_resident ~ is_home + factor(state) +
             factor(candidate) -1, 
           data = ads_remained)

model_labels <- c('All/Dropped/Remained')

table5 <- stargazer(fit1, fit2, fit3, type='text', keep.stat = c('n'), 
          covariate.labels = c('Home', 'Intercept'), omit=c('factor*'),
          dep.var.caption = '', dep.var.labels = model_labels,align = TRUE,
          title = 'Relative proportion of advertising budget', no.space = TRUE,
          add.lines = list(c('State Fixed Effects', 'Yes', 'Yes', 'Yes'),
                           c('Candidate Fixed Effects', 'Yes', 'Yes', 'Yes')))
